package com.rom.stack;


import java.util.Scanner;

public class ArrayStackDemo {
    public static void main(String[] args) {
//        System.out.println('2' - '0');
        ArrayStack arrayStack = new ArrayStack(5);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("show：遍历：");
            System.out.println("exit：退出程序：");
            System.out.println("push：入栈");
            System.out.println("pop： 出栈");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    arrayStack.list();
                    break;
                case 'a':
                    System.out.println("输入一个数：");
                    int value = scanner.nextInt();
                    arrayStack.additem(value);
                    break;
                case 'p':
                    int popitem = arrayStack.popitem();
                    System.out.println(popitem);
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:break;
            }
        }
    }
}

class ArrayStack{

    private int maxSize;
    private int[] stack;
    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[this.maxSize];
    }

    public boolean isFUll(){
        return top == maxSize-1;
    }
    public boolean isEmpty(){
        return top == -1;
    }
    //入栈
    public void additem(int num){
        if(isFUll()){
            System.out.println("栈满");
            return;
        }
        stack[++top] = num;
    }

    //出栈
    public int popitem() {

        if(top == -1){
//            System.out.println("栈空");
            throw new RuntimeException("栈空");
        }
        int value = stack[top--];
        return value;
    }
    //遍历
    public void list(){
        if(isEmpty()){
            System.out.println("栈空");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.println("NO."+stack[i]);
        }
    }
}
